package com.wc.codeforces.二进制.Bitwise_Balancing;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/10/3 23:58
 * @description https://codeforces.com/problemset/problem/2020/C
 */
public class Main {
    /**
     * 思路：
     * b c a d
     * 0 0 0 0
     * 0 0 1 1
     * 0 1 0 0
     * 0 1 1 0
     * 1 0 0 1
     * 1 0 1 1
     * 1 1 0 1
     * 1 1 1 0
     * <p>
     * 总共 b c d的按照位置的状态值有 0 1 2 5 6 7
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static long b, c, d;
    // 打表 b c d => 排列的二进制
    static int[] map = {0, 1, 0, -1, -1, 0, 1, 0};

    public static void main(String[] args) {
        int T = sc.nextInt();
        while (T-- > 0) {
            b = sc.nextLong();
            c = sc.nextLong();
            d = sc.nextLong();
            long res = 0;
            for (int i = 0; i < 61; i++) {
                long x = (b >> i & 1) << 2, y = (c >> i & 1) << 1, z = d >> i & 1;
                if (map[(int) (x + y + z)] == -1) {
                    res = -1;
                    break;
                } else res += (long) map[(int) (x + y + z)] << i;
            }
            out.println(res);
        }
        out.flush();
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
